<?php
session_start();

include("../models/webconfig.php");
include("../models/class.connection.php");
include("../models/entities/class.ProductEntity.php");
include("../models/entities/class.CartEntity.php");
include("../models/entities/class.OrderEntity.php");
include("../models/entities/class.SectionEntity.php");
include("../models/entities/class.CategoryEntity.php");
include("../models/entities/class.AccountEntity.php");
include("../models/entities/class.OrderDetailEntity.php");
include("../models/repositories/class.ProductRepository.php");
include("../models/repositories/class.AccountRepository.php");
include("../models/repositories/class.CartRepository.php");
include("../models/repositories/class.OrderRepository.php");
include("../models/repositories/class.SectionRepository.php");
include("../models/repositories/class.CategoryRepository.php");

$orderRepository = new OrderRepository();
$productRepository = new ProductRepository();
$cartRepository = new CartRepository();
$total = 0;

if (isset($_GET['id'])) {
    $id = $_GET['id'];

    $isInCart = FALSE;
    if (isset($_SESSION['cart'])) {
        if (isset($_SESSION['cart'][$id])) {
            if ($_SESSION['cart'][$id] != 0) {
                $_SESSION['cart'][$id] = $_SESSION['cart'][$id] + 1;
            }
        } else {
            $_SESSION['cart'][$id] = 1;
        }
    } else {
        $_SESSION['cart'][$id] = 1;
    }
}
    if (isset($_SESSION['cart'])) {
        $list = array();
        $index = 0;
        foreach ($_SESSION['cart'] as $key => $value) {
            $product = $productRepository->GetDetail($key);
            $cart = new CartEntity($product->Code_s, $product->Name_s, $product->Price_f, $product->ImageSrc_s, $value);
            $list[] = $cart;
        }

        foreach ($list as $key => $value) {
            $total+=$list[$key]->unitprice * $list[$key]->quantity;
        }
    }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
        <title>Cactus Flower</title>
        <link href="../content/front-end.css" rel="stylesheet" type="text/css" />
        <link href="../content/cart.css" rel="stylesheet" type="text/css" />
        <script src="../scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
        <script src="../scripts/jquery-ui-1.8.6.custom.min.js" type="text/javascript"></script>
        <link href="../content/ui-lightness/jquery-ui-1.8.6.custom.css" rel="stylesheet" type="text/css" />
        <script src="../scripts/jquery.ui.datetimepicker.min.js" type="text/javascript"></script>
        <script src ="../scripts/cart.js" type="text/javascript" ></script>
        <script src="../scripts/search.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                ShoppingCartScript.init();
                CreateOrderScript.init();
                $("#ui-datepicker-div").hide();
            });
        </script>
    </head>
    <body>
        <div id="wrap">
            <?php
            include("../includes/frontend_Header.php");
            include("../includes/frontend_Left.php");
            ?>

            <div id="inside">
                <div id="cartInfo">
                    <h3>Thông tin giỏ hàng</h3>
                </div>
                <center>
                    <div id="shoppingcartMain">
                        <div id="messageCart">
                            <?php
                            if (isset($_SESSION['cart'])) {
                                if (count($_SESSION['cart']) != 0) {
                                    echo "Bạn đang có " . count($_SESSION['cart']) . " sản phẩm trong giỏ hàng";
                                } else {
                                    echo "Bạn chưa có sản phẩm nào trong giỏ hàng.";
                                }
                            } else {
                                echo "Bạn chưa có sản phẩm nào trong giỏ hàng.";
                            }
                            ?></div>
                        <div id="cartDetail">
                            <table width="680px;">
                                <tr>
                                    <td class="title" style="height: 33px"><strong>Sản Phẩm</strong></td>
                                    <td class="title" style="height: 33px"><strong>Đơn Giá /1
						sp</strong></td>
                                    <td class="title" style="height: 33px"><strong>Số Lượng</strong>
                                    </td>
                                    <td class="title" style="height: 33px"><strong>Thành Tiền
						</strong></td>
                                    <td class="title" style="height: 33px"><strong>Xử lý</strong></td>
                                </tr>
                                <?php if (isset($list)) {
                                ?><?php foreach ($list as $key => $value) {
                                ?><?php $imageList = explode("|", $value->imageUrl); ?>
                                        <tr id="tr-<?php echo($value->productCode); ?>" style="font-size: 14px;">
                                            <td>
                                                <div id="img" style="margin: 5px; width: 150px; height: 100px;">
                                                    <img alt="<?php echo($value->productName); ?>" src="../content/images/Products/<?php echo($imageList[0]); ?>" />
                                                </div>
                                                <div id="name" style="margin: 5px; font-weight: bold; color: Teal;">
                                            <?php echo($value->productName); ?></div>
                                        <input id="<?php echo($value->productCode); ?>" name="<?php echo($value->productCode); ?>" type="hidden" value="<?php echo($value->productCode); ?>" />
                                    </td>
                                    <td><?php echo(number_format($value->unitprice, 0)); ?>VND
                                        <input id="unitPrice-<?php echo($value->productCode); ?>" name="unitPrice-<?php echo($value->productCode); ?>" type="hidden" value="<?php echo($value->unitprice); ?>" />
                                    </td>
                                    <td>
                                        <input id="quantity-<?php echo($value->productCode); ?>" name="quantity-<?php echo($value->productCode); ?>" onchange="updateCart('<?php echo($value->productCode); ?>')" style="width: 40px; font-weight: bold;" type="text" value="<?php echo($value->quantity); ?>" />
                                    </td>
                                    <input id="_Total-<?php echo($value->productCode); ?>" name="_Total-<?php echo($value->productCode); ?>" type="hidden" value="<?php echo($value->unitprice * $value->quantity); ?>" />
                                    <td id="total-<?php echo($value->productCode); ?>" class="td-border" style="width: 150px;">
                                        <?php echo(number_format($value->unitprice * $value->quantity, 0)); ?>
                                            <label>VND</label> </td>
                                        <td class="td-border-<?php echo($value->productCode); ?>" style="vertical-align: middle; width: 100px;">
                                            <input id="remove-<?php echo($value->productCode); ?>" onclick="deleteItem('<?php echo($value->productCode); ?>')" src="../content/images/deleteItem.png" style="border: 1px solid #4d4b49; width: 30%; cursor: pointer;" type="image" />
                                        </td>
                                    </tr>
                                <?php } ?><?php } ?>
                                </table>
                            </div>
                            <div id="viewTotal" style="font-weight: bold; margin-top: 5px;">
                                                                        				Tổng cộng : <span id="cartTotal"><?php echo(number_format($total, 0)); ?>
                                    </span>VNĐ <br />
                                    <input id="_cartTotal" name="_cartTotal" type="hidden" value="<?php echo($total); ?>" />
                                </div>
                                <div style="text-align: left; padding: 5px 0px 20px 15px;">
                                    <input id="orderBegin" class="myInput" type="button" value="Tiến hành đặt hàng"/>
                                    <script type="text/javascript">
                                        function GoBack() {
                                            window.location.href = 'index.php';
                                        }
                                    </script>
                                    <input class="myInput" onclick="GoBack()" style="cursor: pointer" type="button" value="Tiếp tục mua hàng" />
                                </div>
                            </div>
                        </center>
                        <form action="cart.php" method="post">
                    <?php
                                    if (isset($_POST['orderName']) && isset($_POST['orderEmail']) &&
                                            isset($_POST['orderPhone']) && isset($_POST['orderCapcha'])) {
                                        if ($_POST['orderName'] != "" && $_POST['orderEmail'] != "" &&
                                                $_POST['orderPhone'] != "" && $_POST['orderCapcha'] != "") {
                                            $capcha = $_POST['orderCapcha'];
                                            if (strcmp($_SESSION['security_code'], $capcha) == 0) {
                                                $name = $_POST['orderName'];
                                                $email = $_POST['orderEmail'];
                                                $phone = $_POST['orderPhone'];
                                                $address = isset($_POST['orderAddress']) ? $_POST['orderAddress'] : "";
                                                $orderTime = date("Y/m/d");
                                                $deliveryTime = isset($_POST['date']) ? $_POST['date'] : date("Y/m/d");
                                                $checked = 0;
                                                $orderEntity = new OrderEntity(0, $name, $email, $address, $phone, $orderTime, $deliveryTime, $checked);
                                                if ($orderRepository->CreateOrder($orderEntity)) {
                                                    echo("<p class='message'>Đơn đặt hàng của bạn đã được tạo thành công</p>");
                                                } else {
                                                    echo("<p class='message'>Có lỗi xảy ra khi gửi thông tin đặt hàng! Vui lòng thử lại.</p>");
                                                }
                                            } else {
                                                echo("<p class='message'>Mã xác nhận không đúng! Vui lòng nhập lại.</p>");
                                            }
                                        } else {
                                            echo "Name=" . $_POST['orderName'] . "email" . $_POST['orderEmail'] . "phone=" . $_POST['orderPhone'] . "capcha" . $_POST['orderCapcha'];
                                            echo("<p class='message'>Bạn chưa điền đầy đủ thông tin cần thiết! Vui lòng thử lại</p>");
                                        }
                                    }
                    ?>

                                    <div id="orderWrap">

                                        <h3>Thông tin đặt hàng</h3>
                                        <table width="100%">
                                            <tr>
                                                <td class="left"><label>Họ và tên </label></td>
                                                <td class="right"><input type="text" name="orderName" id="orderName"/><span class="orderNameMsg"> (*) </span> </td>
                                            </tr>
                                            <tr>
                                                <td class="left"><label>Email </label></td>
                                                <td class="right"><input type="text" name="orderEmail" id="orderEmail" /><span class="orderEmailMsg"> (*) </span> </td>
                                            </tr>
                                            <tr>
                                                <td class="left"><label>Địa chỉ </label></td>
                                                <td class="right"><input type="text"  name="orderAddress" id="orderAddress" /></td>
                                            </tr>
                                            <tr>
                                                <td class="left"><label>Số điện thoại </label></td>
                                                <td class="right"><input type="text"  name="orderPhone" id="orderPhone" /><span class="orderPhoneMsg"> (*) </span> </td>
                                            </tr>

                                            <tr>
                                                <td class="left"><label>Thời gian giao hàng </label></td>
                                                <td class="right"><input type="text" id="date" name="date" /> <span class="dateMsg"></span></td>
                                            </tr>
                                            <tr>
                                                <td class="left"><label>Mã xác nhận </label></td>
                                                <td class="right"><img alt="Nhập mã xác nhận" src="../library/random_image.php" /></td>
                                            </tr>
                                            <tr>
                                                <td class="left"><label>Nhập mã xác nhận </label></td>
                                                <td class="right"><input type="text"  name="orderCapcha" id="orderCapcha" /><span class="orderCapchaMsg"> (*) </span> </td>
                                            </tr>
                                            <tr>
                                                <td id="tdSubmit" colspan="2" class="mid"></td>
                                            </tr>
                                        </table>
                                    </div>
                                </form>
                            </div>

                        </div>

        <?php
                                    include("../includes/frontend_footer.php");
        ?>

    </body>

</html>

